package com.example.demo.controller;

import com.example.demo.pojo.Case;
import com.example.demo.service.CaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller("caseController")
@RequestMapping("/case")
public class CaseController {
    @Autowired
    CaseService caseService;

    @RequestMapping("/tocase")
    public String toCase(){
        return "case";
    }

    @RequestMapping("/showcase")
    @ResponseBody
    public List<Case> showCase(){
        return caseService.showAll();
    }

    @RequestMapping("/searchcase")
    @ResponseBody
    public List<Case> searchCase(String case_id){
        System.out.println("searchCase" + case_id);
        int id = Integer.valueOf(case_id).intValue();
        return caseService.searchCase(id);
    }

    @RequestMapping("/toaddcase")
    public String toAddCase(){
        return "add_case";
    }

    @PostMapping ("/addcase")
    @ResponseBody
    public int AddCase(@RequestBody Case newcase, HttpSession session){
        String doctor_id = (String) session.getAttribute("username");
        newcase.setDoctor_id("103");
        System.out.println(newcase);
        int flag = caseService.addCase(newcase);
        System.out.println(flag);
        return flag;
    }
}
